#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e6 + 5;
const int T = 1005;

int t, n, id;
queue<int> core;
queue<int> sub[T];
int team[N];

void push(int x) {
  int tid = team[x];
  if (sub[tid].empty()) core.push(tid);
  sub[tid].push(x);
}
void pop() {
  int tid = core.front();
  sub[tid].pop();
  if (sub[tid].empty()) core.pop();
}
int front() {
  int tid = core.front();
  return sub[tid].front();
}
void clear() {
  while (core.size()) {
    int tid = core.front();
    core.pop();
    while (sub[tid].size()) sub[tid].pop();
  }
}
bool solve() {
  cin >> t;
  if (!t) return false;
  cout << "Scenario #" << ++id << endl;
  rep(i, 1, t) {
    cin >> n;
    rep(j, 1, n) {
      int x;
      cin >> x;
      team[x] = i;
    }
  }
  string op;
  int x;
  while ((cin >> op) && op != "STOP") {
    if (op == "ENQUEUE") {
      cin >> x;
      push(x);
    } else {
      cout << front() << endl;
      pop();
    }
  }
  clear();
  cout << endl;
  return true;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  while (solve());
  return 0;
}